package com.cykj.service.impl;

import com.cykj.mapper.MenuMapper;
import com.cykj.mapper.RoleMapper;
import com.cykj.pojo.CyMenu;
import com.cykj.pojo.CyRole;
import com.cykj.service.RoleService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.cykj.util.TreeMenuUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public ResponseDto getAllRoles(int pageCurr,int pageSize) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<CyRole> allRoles = roleMapper.getAllRoles();

        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto getPermissionMenu(int roleId) {
        //已分配权限的菜单
        List<CyMenu> permissionMenu = menuMapper.getPermissionMenu(roleId);

        //查询所有菜单
        List<CyMenu> allMenu = menuMapper.getAllMenu();
        //执行过滤操作，得到未分配的菜单 === fromData
        //如果左侧菜单没有孩子，对比右侧，如果存在，那就可用直接删除
        //如果左侧菜单有孩子，对比右侧，如果存在，不能马上删除，要先看他的孩子
        filterMenus(allMenu, permissionMenu);


        List<CyMenu> fromData = TreeMenuUtils.build(allMenu);

        List<CyMenu> toData = TreeMenuUtils.build(permissionMenu);
        HashMap<String, Object> map = new HashMap<>();
        map.put("toData",toData);
        map.put("fromData",fromData);
        return new ResponseDto(1,"查询成功",map);
    }

    @Override
    public ResponseDto addPermission(int menuId, int roleId) {
        int i = roleMapper.addPermission(menuId, roleId);
        if (i == 0){
            return new ResponseDto(0,"添加失败",null);
        }else {
            return new ResponseDto(1,"添加成功",null);
        }
    }

    @Override
    public ResponseDto deletePermission(int menuId, int roleId) {
        int i = roleMapper.deletePermission(menuId, roleId);
        if (i == 0){
            return new ResponseDto(0,"删除失败",null);
        }else {
            return new ResponseDto(1,"删除成功",null);
        }
    }

    private void filterMenus(List<CyMenu> fromData,List<CyMenu> toData){
        //迭代器迭代菜单
        Iterator<CyMenu> iterator = fromData.iterator();
        while (iterator.hasNext()){
            CyMenu next = iterator.next();
            if (next.getChildren().size() == 0){
                //    说明这个已经拆分到没有子级菜单了，那我们就需要去跟toData里面的去比较
                //这边是将toData当作数组
                for (CyMenu toDatum : toData) {
                    //    遍历toData里面的每一个数据
                    if (next.getMenuId() == toDatum.getMenuId()){
                        iterator.remove();
                        break;
                    }
                }
            }else {
                filterMenus(next.getChildren(),toData);
                if (next.getChildren().size() == 0){
                    iterator.remove();
                }
            }
        }
    }
}
